﻿namespace Text2Rss.Migrations.Release001
{
	#region Using Directives
	using System;
	using System.Data;
	using System.Security.Cryptography;
	using System.Text;
	using ECM7.Migrator.Framework;
	#endregion

	/// <summary>
	/// Creates Users Table and adds admin
	/// </summary>
	[Migration(1)]
	public class Migration001_Users : Migration
	{
		public override void Apply()
		{
			Database.AddTable(
				"Users",
				new Column("Email", DbType.String.WithSize(100), ColumnProperty.PrimaryKey),
				new Column("Version", DbType.Int32, ColumnProperty.NotNull),
				new Column("Name", DbType.String.WithSize(200), ColumnProperty.NotNull, "''"),
				new Column("PasswordHash", DbType.String.WithSize(100), ColumnProperty.NotNull, "''"),
				new Column("IsAdmin", DbType.Boolean, ColumnProperty.NotNull, "'False'"));

			byte[] bytes = Encoding.UTF8.GetBytes("tN920U");
			using (var md5 = MD5.Create())
			{
				byte[] hashBytes = md5.ComputeHash(bytes);
				string hashString = Convert.ToBase64String(hashBytes);
				Database.Insert(
					"Users",
					new[] { "Email", "Version", "Name", "PasswordHash", "IsAdmin" },
					new[] { "admin", "1", "Admin", hashString, "True" });
			}
		}

		public override void Revert()
		{
			Database.RemoveTable("Users");
		}
	}
}
